<template>
  <div>
    <div class="bg">
      <div class="containers">
        <div class="left">
        </div>
        <div class="right">
          <p class="title">职统</p>
          <p class="content">企业高效办公平台</p>
          <el-form :model="register" status-icon :rules="rules" ref="register" label-width="70px" class="register-form">
            <el-form-item label="用户名" prop="username">
              <el-input placeholder="请输入用户名" type="text" v-model="register.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="员工姓名" prop="employeename">
              <el-input placeholder="请输入员工姓名" type="text" v-model="register.employeename" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="电话号码" prop="phone">
              <el-input placeholder="请输入电话号码" type="text" v-model="register.phone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱地址" prop="email">
              <el-input placeholder="请输入邮箱地址" type="text" v-model="register.email" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="部门" prop="department">
              <el-select v-model="register.department" placeholder="请选择">
                <el-option
                  v-for="item in register.departments"
                  :key="item.departmentid"
                  :label="item.departmentname"
                  :value="item.departmentid">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input placeholder="请输入密码" type="password" v-model="register.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPassword">
              <el-input placeholder="请再次输入密码" type="password" v-model="register.checkPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit('register')">注册</el-button>
              <el-button @click="reset('login')">返回</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name:'regusterPage',
  data() {
    let validateUserName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    };
    let validateDepartment = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请选择部门'));
      } else {
        callback();
      }
    };
    let validateEmployeename = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入姓名'));
      } else {
        callback();
      }
    };
    let validatePhone = (rule, value, callback) => {
      if (value === '') {
          callback(new Error('请输入手机号码'));
      } else {
        if (!(/^1[0-9]{10}$/).test(value)) {
          callback(new Error('请输入正确的手机号码'));
        } else 
          callback();
      }
    };
    let validateEmail = (rule, value, callback) => {
      if (value === '') {
          callback(new Error('请输入邮箱'));
      } else {
        if (!(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/).test(value)) {
          callback(new Error('请输入正确的邮箱地址'));
        }else
          callback();
      }
    };
    let validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.register.password !== '') {
          this.$refs.register.validateField('checkPassword');
        }
        callback();
      }
    };
    let validatePassword2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.register.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      register: {
        username: '',
        password: '',
        checkPassword: '',
        employeename: '',
        phone: '',
        email: '',
        //下拉框
        departments: [],
        department: 1,
      },
      rules: {
        username: [
          { validator: validateUserName, trigger: 'blur' }
        ],
        department: [
          { validator: validateDepartment, trigger: 'blur' }
        ],
        employeename: [
          { validator: validateEmployeename, trigger: 'blur' }
        ],
        phone: [
          { validator: validatePhone, trigger: 'blur' }
        ],
        email: [
          { validator: validateEmail, trigger: 'blur' }
        ],
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        checkPassword: [
          { validator: validatePassword2, trigger: 'blur' }
        ]
      }
    };
  },
  async mounted() {
    let {code ,data, msg} = await this.$post('/register')
    if(code === 200) {
      this.register.departments = data
    }else {
      this.$message.error(msg)
    }
  },
  methods: {
    async submit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let {code,msg} = await this.$post('/doreg', {
            data: {
              employeename: this.register.employeename,
              username: this.register.username,
              phone: this.register.phone,
              email: this.register.email,
              password: this.register.password,
              departmentid: this.register.department,
              status: 0,
              role: 0,
              awardtimes: 0
            }
          })
          if(code === 200){
            this.$message.success(msg)
            this.$router.push('/login')
          }else {
            this.$message.error(msg)
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    reset(formName) {
      // this.$refs[formName].resetFields();
      console.log(formName)
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped lang='scss'>
.bg {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('~@/assets/mainicon/bg.png');
  background-size: 100vw 100vh;
  overflow: hidden;

  .containers {
    width: 600px;
    min-height: 550px;
    border-radius: 20px;
    justify-content: center;
    align-items: center;
    background:white;
    display: flex;

    .left {
      width: 200px;
      height: 200px;
      background: url('~@/assets/mainicon/bgbg.png');
      background-size: 200px 200px;
    }

    .right {
      width: 300px;

      .title {
        // text-align: center;
        font-size: 20px;
      }

      .content{
        // text-align: center;
        margin-top: 20px;
        margin-bottom: 20px;
        font-size: 12px;
        color: #b4b4b4;
      }

      .register-form {
        width: 300px;

        .buttons {
          display: flex;
        }
      }
    }
  }
}


</style>